package com.zgkxzx.java.mutilThread.forkAndJoin;

import java.util.List;
import java.util.concurrent.RecursiveAction;

/**
 * 创建者 zgkxzx
 * 日期   1/3/18
 * 描述
 */

public class ProductTask extends RecursiveAction {

    private List<Product> products;
    private int first;
    private int last;

    private double increase;

    public ProductTask(List<Product> products, int first, int last, double increase) {
        this.products = products;
        this.first = first;
        this.last = last;
        this.increase = increase;
    }

    @Override
    protected void compute() {
        if (last - first < 10) {
            updateProducts();
        } else {
            int middle = (last + first) / 2;

            System.out.println(" QueuedTaskCount " + getQueuedTaskCount());
            ProductTask productTask = new ProductTask(products, first, middle, increase);
            ProductTask productTask2 = new ProductTask(products, middle , last, increase);

            invokeAll(productTask, productTask2);
        }

    }

    private void updateProducts() {
        for (int i = first; i < last; i++) {
            Product product = products.get(i);
            product.setPrice(product.getPrice() * (1 + increase));
        }

    }
}
